package cn.com.infcn.monitor.task.jvm;

import java.lang.management.ClassLoadingMXBean;
import java.lang.management.ManagementFactory;

import org.apache.log4j.Logger;

import cn.com.infcn.monitor.bean.IFCJVMClassLoading;
import cn.com.infcn.monitor.task.BaseTask;
import cn.com.infcn.monitor.util.IFCBeanUtils;

/**
 * 用于 Java 虚拟机的类加载系统的管理接口。
 * 
 * @author jjs
 *
 */
public class JVMClassLoadingTask implements BaseTask {

	private static final Logger LOGGER = Logger.getLogger(JVMClassLoadingTask.class);

	@Override
	public IFCJVMClassLoading execute() throws Exception {
		return jvmClassLoading();
	}

	public IFCJVMClassLoading jvmClassLoading() {
		try {
			ClassLoadingMXBean classLoadingMXBean = ManagementFactory.getClassLoadingMXBean();

			IFCJVMClassLoading ifcClassLoading = new IFCJVMClassLoading();

			IFCBeanUtils.copyProperties(classLoadingMXBean, ClassLoadingMXBean.class, ifcClassLoading);
			
			return ifcClassLoading;
		} catch (Exception e) {
			LOGGER.warn(e.getMessage(), e);
		}

		return null;
	}
	
	/*private void copyProperties(ClassLoadingMXBean classLoadingMXBean, IFCJVMClassLoading ifcClassLoading){
		ifcClassLoading.setIsVerbose(classLoadingMXBean.isVerbose());
		ifcClassLoading.setLoadedClassCount(classLoadingMXBean.getLoadedClassCount());
		ifcClassLoading.setTotalLoadedClassCount(classLoadingMXBean.getTotalLoadedClassCount());
		ifcClassLoading.setUnloadedClassCount(classLoadingMXBean.getUnloadedClassCount());
	}*/

	public static void main(String[] args) {
		JVMClassLoadingTask task = new JVMClassLoadingTask();
		IFCJVMClassLoading ifcClassLoading = task.jvmClassLoading();

		System.out.println(ifcClassLoading.getLoadedClassCount());
		System.out.println(ifcClassLoading.getTotalLoadedClassCount());
		System.out.println(ifcClassLoading.getUnloadedClassCount());
		System.out.println(ifcClassLoading.getIsVerbose());
	}

}
